class Cell extends egret.Sprite {
	public static MIN_PANIC_POWER: number;
	public static MIN_ATTACK_POWER: number;
	public static GREY_GIVE_UP_DIV: number;
	public static MAX_POWER_CREDIT: number;
	public static TYPE_GREY: number;
	public static TYPE_RED: number;
	public static TYPE_GREEN: number;
	public static TYPE_BLACK: number;
	public static TYPE_PURPLE: number;
	private _SafeStr_104: number = 18;
	private m_game: Level;
	private _attackInc: number = 0;
	private _pause: boolean = false;
	/**力量增量***/
	private _powerDelta: number = 0;
	private _maxPower: number = 0;
	private anchorList: Array<any> = null;
	private hintSprite: egret.Sprite = null;
	private origLinkList: Array<any> = null;
	private moveDeltaY: number = 0;
	private _SafeStr_100: number = 1;
	private _SafeStr_105: number = 0;
	private _pulsar: Pulsar = null;
	private sfx: Sfx = null;
	private moveDeltaX: number = 0;
	private evoInfoList: Array<any> = null;
	private _power: number = 0;
	private _enabled: boolean = false;
	private _growInc: number = 0;
	private _growPeriod: number = 20;
	private _type: number = 0;
	private bgSprite: egret.Sprite = null;
	private cellNodeList: Array<any> = null;
	private _SafeStr_112: number = 2;
	private _attackPeriod: number = 20;
	private _attackDelta: number = 0;
	private _occupantPower: number = 0;
	private _curEvoIndex: number = 0;

	public constructor(game: Level, type: number, initPower: number, maxPower: number = 999) {
		super();
		this.m_game = game;
		this._type = type;
		this._power = initPower;
		this._maxPower = maxPower;
		this.anchorList = new Array();
		this.origLinkList = new Array();
		this.cellNodeList = new Array();
		this.evoInfoList = new Array();
		this.sfx = new Sfx();
		this.bgSprite = new egret.Sprite();
		this._curEvoIndex = 0;
		if (this._power > this._maxPower) {
			this._power = (this._maxPower);
		}
		this.bgSprite.touchEnabled = false;
		this.bgSprite.touchChildren = false;
		this.addChild(this.sfx);
		this.addChild(this.bgSprite);
		/**进化列表**/
		this.evoInfoList[0] = new EvoInfo(15, CellNode.TYPE1, -1, 0, 40, 30, ("spore").toLocaleUpperCase(), 1);
		this.evoInfoList[CellNode.TYPE1] = new EvoInfo(40, CellNode.TYPE2, 5, 0, 45, 20, ("embryo").toLocaleUpperCase(), 2);
		this.evoInfoList[CellNode.TYPE2] = new EvoInfo(80, CellNode.TYPE3, 30, CellNode.TYPE1, 50, 15, ("pulsar").toLocaleUpperCase() + "-A", 2);
		this.evoInfoList[CellNode.TYPE3] = new EvoInfo(120, CellNode.TYPE4, 60, CellNode.TYPE2, 60, 10, ("pulsar").toLocaleUpperCase() + "-B", 2);
		this.evoInfoList[CellNode.TYPE4] = new EvoInfo(160, CellNode.TYPE5, 100, CellNode.TYPE3, 80, 5, ("ant").toLocaleUpperCase(), 3);
		this.evoInfoList[CellNode.TYPE5] = new EvoInfo(210, CellNode.TYPE5, 140, CellNode.TYPE4, 100, 2, ("predator").toLocaleUpperCase(), 3);
		do
		{ }
		while (this.updateCell())
		this.draw();
		this.addEventListener(egret.Event.ENTER_FRAME, this.onEnterFrame, this);
	}

	public get maxPower(): number {
		return (this._maxPower);
	}

	public set maxPower(value: number) {
		this._maxPower = value;
		if (this._power > this._maxPower) {
			this._power = (this._maxPower);
			do
			{ }
			while (this.updateCell())
			this.draw();
		}
	}

	public get maxOrigLinks(): number {
		var info: EvoInfo = this.evoInfoList[this._curEvoIndex];
		return (info.maxLinks);
	}

	public get power(): number {
		return (this._power);
	}

	public getAnchorTo(cell: Cell): Anchor {
		var ang: number = 0;
		var STEP: number = (10);
		var dx: number = (cell.x - this.x);
		var dy: number = (cell.y - this.y);
		var dir: number = ((Math.atan2(dy, dx) * 180) / Math.PI);
		if (dir < 0) {
			dir = (360 + dir);
		}
		var index: number = (((dir / STEP)));
		var anchor: Anchor = this.anchorList[index];
		var radius: number = this.radius();
		if (anchor == null) {
			ang = (((index * STEP) * Math.PI) / 180);
			anchor = new Anchor((this.x + (Math.cos(ang) * radius)), (this.y + (Math.sin(ang) * radius)));
			this.anchorList[index] = anchor;
		}
		return (anchor);
	}

	public set power(value: number) {
		if (value > this._maxPower) {
			value = (this._maxPower);
		}
		this._power = (value);
		do
		{ }
		while (this.updateCell())
		this.draw();
	}

	public addImpulse(dir: number, value: number = 1) {
		var rad: number = ((dir * Math.PI) / 180);
		var mass: number = (this._power / 20);
		if (mass < 1) {
			mass = 1;
		}
	}

	private draw() {
		var step: number = 0;
		var begin: number = 0;
		var node: CellNode = null;
		var color: number = (GUI.OUTLINE_COLOR);
		var bgcolor: number = (GUI.INNER_COLOR);
		var centerColor: number = (this.baseColor());
		var g: egret.Graphics = this.bgSprite.graphics;
		g.clear();
		g.lineStyle(2, color);
		g.beginFill(bgcolor, 0.3);
		g.drawCircle(0, 0, this.radius());
		g.endFill();
		g.lineStyle(2, color);
		g.beginFill(centerColor, ((this._power < Cell.MIN_ATTACK_POWER) ? (this._power / Cell.MIN_ATTACK_POWER) : 1));
		g.drawCircle(0, 0, 15);
		g.endFill();
		if (this.hintSprite) {
			this.bgSprite.removeChild(this.hintSprite);
			this.hintSprite = null;
		}
		var i: number = 0;
		if (this._type != Cell.TYPE_GREY) {
			this.hintSprite = GUI.createText(("" + this._power), 15, 0xFFFFFF);
			this.hintSprite.y = -3;
			this.bgSprite.addChild(this.hintSprite);
			step = 5;
			begin = (-((this.maxOrigLinks * step) / 2) + (step / 2));
			g.lineStyle(1, color);
			i = (0);
			while (i < this.maxOrigLinks) {
				g.beginFill(((i >= this.numOrigLinks) ? 0xAAAAAA : 0));
				g.drawCircle((begin + (i * step)), 7, 2.5);
				g.endFill();
				i++;
			}
		}
		this.updateCell();
		this.updateAnchor();
		i = 0;
		while (i < this.cellNodeList.length) {
			node = this.cellNodeList[i];
			node.setRadius(this.radius());
			i++;
		}
	}

	public destruct() {
		var link: Link;
		if (!this._enabled) {
			this.removeEventListener(egret.Event.ENTER_FRAME, this.onEnterFrame, this);
		}
		while (this.origLinkList.length > 0) {
			link = this.origLinkList.pop();
			link.destruct();
		}
		this.clearCellNode();
		this.m_game.delCell(this);
	}

	public getOccupantPower(): number {
		return (this._occupantPower);
	}

	public receiveAttack(type: number, amount: number) {
		var _local_4: number = 0;
		/*被俘虏**/
		var captured: boolean;
		if (type != this._type) {
			if (this._type != Cell.TYPE_GREY) {
				this._power = ((this._power - amount));
				if (this._power <= 0) {
					this._power = (10);
					captured = true;
				}
			}
			else {
				this._occupantPower = ((this._occupantPower + ((this._SafeStr_100 == type) ? amount : -(amount))));
				if (this._occupantPower < 0) {
					this._SafeStr_100 = (type);
					this._occupantPower = (-(this._occupantPower));
					if (this._pulsar) {
						this.removeChild(this._pulsar);
						this._pulsar.destruct();
						this._pulsar = null;
					}
					this._pulsar = new Pulsar(0, 0, this.baseColor(type), 0);
					this.addChild(this._pulsar);
				}
				_local_4 = (((this._power / Cell.GREY_GIVE_UP_DIV)));
				if (this._occupantPower >= _local_4) {
					if (this._power < 10) {
						this._power = (10);
					}
					captured = true;
					if (this._pulsar) {
						this.removeChild(this._pulsar);
						this._pulsar.destruct();
						this._pulsar = null;
					}
				}
				if (this._pulsar) {
					this._pulsar.setSize(((this._occupantPower * 10) / _local_4));
				}
			}
		}
		else {
			this.increasePower(amount);
		}
		if (captured) {
			this._type = (type);
			this._curEvoIndex = (0);
			do
			{ }
			while (this.updateCell())
			this.cutAllLink();
			this.sfx.startWave(this.radius());
			// SoundMgr.playSfx(SoundMgr.CELL_OCCUPIED);
		}
		this.draw();
	}

	private cutAllLink() {
		var tmp: Array<Link> = new Array();
		tmp = this.origLinkList.concat();
		var link: Link;
		// var i: number = 0;
		// while (i < this.origLinkList.length) {
		// 	link = this.origLinkList[i];
		// 	tmp.push(link);
		// 	i++;
		// }

		while (tmp.length > 0) {
			link = tmp.pop();
			link.cutAt(1);
		}
	}

	public get numOrigLinks(): number {
		return (this.origLinkList.length);
	}

	public getGiveUpPower(): number {
		return ((this._type == Cell.TYPE_GREY) ? (((this._power / Cell.GREY_GIVE_UP_DIV) - this._occupantPower)) : this._power);
	}

	public radius(): number {
		var r: number = (this._SafeStr_104 + (this._power / 10));
		return (r);
	}

	public delOriginLink(link: Link) {
		var i: number = (this.origLinkList.indexOf(link));
		if (i >= 0) {
			this.origLinkList.splice(i, 1);
		}
		this.draw();
	}

	public getName(): string {
		var color: string;
		var info: EvoInfo = this.evoInfoList[this._curEvoIndex];
		switch (this._type) {
			case Cell.TYPE_GREY:
				color = "";
				break;
			case Cell.TYPE_RED:
				color = "";
				break;
			case Cell.TYPE_GREEN:
				color = "";
				break;
			case Cell.TYPE_BLACK:
				color = "";
				break;
			case Cell.TYPE_PURPLE:
				color = "";
				break;
		}
		return (color + info.name);
	}

	private updateCell(): boolean {
		var i: number = 0;
		var node: CellNode;
		var changed: boolean = false;
		var prevEvo: number = this._curEvoIndex;
		var info: EvoInfo = this.evoInfoList[this._curEvoIndex];
		var power: number = (this._power);
		if (power >= info.upgradePower) {
			this._curEvoIndex = (info.upgrageType);
		}
		else {
			if (power <= info.downgradePower) {
				this._curEvoIndex = (info.downgradeType);
			}
		}
		info = this.evoInfoList[this._curEvoIndex];
		this._growPeriod = info.growPeriod;
		this._attackPeriod = info.attackPeriod;
		if (prevEvo != this._curEvoIndex) {
			changed = true;
			this.clearCellNode();
			if (this._curEvoIndex != 0) {
				i = (0);
				while (i < 360) {
					node = new CellNode(this._curEvoIndex, this.radius(), i, this.baseColor());
					this.cellNodeList.push(node);
					this.addChild(node);
					i = ((i + 45));
				}
			}
			this.sfx.startWave(this.radius());
		}
		if (((this._type == this.m_game.getUserCellType()) && (this._curEvoIndex == CellNode.TYPE5))) {
			// this.m_game.newAchievement(Achievements.ID_TOP_LIFE_FORM);
		}
		return changed;
	}

	public baseColor(_arg_1: number = -1): number {
		var color: number;
		if (_arg_1 == -1) {
			_arg_1 = (this._type);
		}
		switch (_arg_1) {
			case Cell.TYPE_GREY:
				color = (0x888888);
				break;
			case Cell.TYPE_RED:
				color = (11154227);
				break;
			case Cell.TYPE_GREEN:
				color = (3385907);
				break;
			case Cell.TYPE_BLACK:
				color = (0x111111);
				break;
			case Cell.TYPE_PURPLE:
				color = (11154346);
				break;
		}
		return (color);
	}

	private increasePower(amount: number = 1) {
		if (this._powerDelta > 0) {
			this._powerDelta = ((this._powerDelta - amount));
			if (this._powerDelta < 0) {
				this._power = ((this._power - this._powerDelta));
				this._powerDelta = (0);
			}
		}
		else {
			this._power++;
		}
		if (this._power > this._maxPower) {
			this._power = (this._maxPower);
		}
		this.draw();
	}

	private onEnterFrame(event: egret.Event) {
		this.moveDelta();
		if (this._pause) {
			return;
		}
		if (this._type != Cell.TYPE_GREY) {
			this._growInc++;
		}
		var growPeriod: number = (((this._attackDelta > 0) ? (this._growPeriod * this._attackDelta) : this._growPeriod));
		if (this._growInc > growPeriod) {
			this._growInc = (0);
			this.increasePower();
		}
		this._SafeStr_105++;
		if (this._SafeStr_105 > this._SafeStr_112) {
			this._SafeStr_105 = (0);
			this._SafeStr_117();
		}
		this._attackInc++;
		if (this._attackInc > this._attackPeriod) {
			this._attackInc = (0);
			this.sendAttack();
		}
	}

	public addOriginLink(link: Link) {
		this.origLinkList.push(link);
		this.sfx.startWave(this.radius());
		this.draw();
	}

	public suspend() {
		var link: Link;
		if (this._pause) {
			return;
		}
		this._pause = true;
		var i: number = 0;
		while (i < this.origLinkList.length) {
			link = this.origLinkList[i];
			link.cutAt(1);
			i++;
		}
	}

	public get type(): number {
		return (this._type);
	}

	public addMoveDelta(dir: number, value: number = 1) {
		var rad: number = ((dir * Math.PI) / 180);
		this.moveDeltaX = (this.moveDeltaX + (Math.cos(rad) * value));
		this.moveDeltaY = (this.moveDeltaY + (Math.sin(rad) * value));
	}

	private updateAnchor() {
		var anchor: Anchor;
		var ang: number = 0;
		var i: number = 0;
		while (i <= 36) {
			anchor = this.anchorList[i];
			ang = (((i * 10) * Math.PI) / 180);
			if (anchor) {
				anchor.update((this.x + (Math.cos(ang) * this.radius())), (this.y + (Math.sin(ang) * this.radius())));
			}
			i++;
		}
	}

	private moveDelta() {
		this.moveDeltaX = (this.moveDeltaX * 0.6);
		this.moveDeltaY = (this.moveDeltaY * 0.6);
		this.x = (this.x + this.moveDeltaX);
		this.y = (this.y + this.moveDeltaY);
		if (((Math.abs(this.moveDeltaX) > 0) || (Math.abs(this.moveDeltaY) > 0))) {
			this.updateAnchor();
		}
	}

	private clearCellNode() {
		var node: CellNode;
		while (this.cellNodeList.length > 0) {
			node = this.cellNodeList.pop();
			this.removeChild(node);
			node.destruct();
			node = null;
		}
	}

	private sendAttack() {
		var link: Link;
		var isUpdateView: boolean;
		var attacksNum: number = 0;
		var i: number = 0;
		while (i < this.origLinkList.length) {
			link = this.origLinkList[i];
			if (this._power < Cell.MIN_ATTACK_POWER) {
				break;
			}
			if (link.isAttached()) {
				if (link.sendAttack(1)) {
					attacksNum++;
				}
			}
			i++;
		}
		if (this._power < 0) {
			this._power = (0);
		}
		if (isUpdateView) {
			this.draw();
		}
		this._attackDelta = ((attacksNum * 2));
	}

	public getAngleTo(cell: Cell): number {
		var dx: number = (cell.x - this.x);
		var dy: number = (cell.y - this.y);
		return ((Math.atan2(dy, dx) * 180) / Math.PI);
	}

	public set type(value: number) {
		value = (value);
		var tmpPower: number = (this._power);
		this._power = (1);
		do
		{ }
		while (this.updateCell())
		this._power = (tmpPower);
		this._type = (value);
		do
		{ }
		while (this.updateCell())
		this.draw();
	}

	private _SafeStr_117() {
		var link: Link;
		var isUpdateView: boolean;
		var i: number = 0;
		while (i < this.origLinkList.length) {
			link = this.origLinkList[i];
			if (!link.isBusy()) {
				if (link.addSeg()) {
					if (this._power > 0) {
						this._power--;
					}
					else {
						this._powerDelta++;
					}
					isUpdateView = true;
				}
				if (((!(link.isAttached())) && (this._powerDelta > Cell.MAX_POWER_CREDIT))) {
					link.cutAt(1);
					this.sfx.startWave(this.radius());
				}
			}
			i++;
		}
		if (isUpdateView) {
			this.draw();
		}
	}

	public pause(enabled: boolean) {
		if (this._enabled == enabled) {
			return;
		}
		this._enabled = enabled;
		if (this._enabled) {
			this.removeEventListener(egret.Event.ENTER_FRAME, this.onEnterFrame, this);
		}
		else {
			this.addEventListener(egret.Event.ENTER_FRAME, this.onEnterFrame, this);
		}
	}

}

class EvoInfo extends egret.HashObject {
	public attackPeriod: number = 0;
	public name: string = "";
	public downgradeType: number = 0;
	public downgradePower: number = 0;
	public upgrageType: number = 0;
	public growPeriod: number = 0;
	public maxLinks: number = 0;
	public upgradePower: number = 0;

	public constructor(upgradePower: number, upgrageType: number, downgradePower: number, downgradeType: number, growPeriod: number, attackPeriod: number, name: string = "", maxLinks: number = 1) {
		super();
		this.upgradePower = (upgradePower);
		this.upgrageType = (upgrageType);
		this.downgradePower = (downgradePower);
		this.downgradeType = (downgradeType);
		this.growPeriod = (growPeriod);
		this.attackPeriod = (attackPeriod);
		this.name = name;
		this.maxLinks = (maxLinks);
	}

}

Cell.MIN_PANIC_POWER = 5;
Cell.MIN_ATTACK_POWER = 5;
Cell.GREY_GIVE_UP_DIV = 3;
Cell.MAX_POWER_CREDIT = 0;
Cell.TYPE_GREY = 1;
Cell.TYPE_RED = 2;
Cell.TYPE_GREEN = 3;
Cell.TYPE_BLACK = 4;
Cell.TYPE_PURPLE = 5;

